package com.redteam.xomtro;

import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

/**
 * Created by AnhThuanIT on 6/13/2014.
 */
public class DetailMapActivity extends MasterActivity {
    public static String LOCATION_NAME = "location_name";
    public static String ADDRESS = "address";
    public static String LATITUDE = "latitude";
    public static String LONGITUDE = "longitude";

    private GoogleMap googleMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String locationName = getIntent().getStringExtra(LOCATION_NAME);
        String address = getIntent().getStringExtra(ADDRESS);
        double latitude = getIntent().getDoubleExtra(LATITUDE, 0);
        double longitude = getIntent().getDoubleExtra(LONGITUDE, 0);

        setContentView(R.layout.item_room_details_map_expanded);
        initilizeMap();
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        googleMap.getUiSettings().setCompassEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);
        googleMap.getUiSettings().setAllGesturesEnabled(true);
        googleMap.setMyLocationEnabled(true);
        googleMap.setTrafficEnabled(true);

        CameraPosition cameraPosition = new CameraPosition.Builder().target(
                new LatLng(latitude, longitude)).zoom(16f).build();
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        // create and add marker
        googleMap.addMarker(new MarkerOptions()
                .title(address)
                .position(new LatLng(latitude, longitude))
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_marker_flooding))).showInfoWindow();

        if (locationName.trim().length() > 0)
            setTitle(locationName);
        else
            setTitle(address);
    }

    private void initilizeMap() {
        if (googleMap == null) {
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.roomMapDetailsFragment);
            if (mapFragment != null)
                googleMap = mapFragment.getMap();
        }
    }

}
